package com.gilight.base.service.impl;

import com.gilight.base.common.Constant;
import com.gilight.base.component.CacheComponent;
import com.gilight.base.component.CacheConst;
import com.gilight.base.dto.*;
import com.gilight.base.model.ColdProduct;
import com.gilight.base.dao.ColdProductMapper;
import com.gilight.base.service.IColdProductService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author shr
 * @since 2021-07-15
 */
@Service
public class ColdProductServiceImpl extends ServiceImpl<ColdProductMapper, ColdProduct> implements IColdProductService {

    @Autowired
    private ColdProductMapper coldProductMapper;
    @Autowired
    private CacheComponent cacheComponent;
    @CacheEvict(value = Constant.PORDUCT_CACHE_LIST)
    @Transactional
    @Override
    public void saveProduct(ColdProductDto coldProductDto) {
        //添加基本信息
        coldProductMapper.saveProductBase(coldProductDto);
        //添加标签信息
        this.saveProductTag(coldProductDto.getProductId(),coldProductDto.getColdProductTag());
        //添加轮播图
        this.saveProductImg(coldProductDto.getProductId(),coldProductDto.getColdProductImg());
        //添加规格
        this.saveParameter(coldProductDto.getProductId(),coldProductDto.getColdProductParameter());
    }

    @CacheEvict(value = Constant.PORDUCT_CACHE_LIST,allEntries=true)
    @Override
    public void saveProductTag(Long productId,List<ColdProductTagDto> tagList) {
        if(CollectionUtils.isNotEmpty(tagList)){
            for (ColdProductTagDto coldProductTagDto:tagList) {
                coldProductTagDto.setProductId(productId);
                coldProductMapper.saveProductTag(coldProductTagDto);
            }
        }
    }

    @CacheEvict(value = Constant.PORDUCT_CACHE_LIST,allEntries=true)
    @Override
    public void saveProductImg(Long productId,List<ColdProductImgDto> imgList) {
        if(CollectionUtils.isNotEmpty(imgList)){
            for (ColdProductImgDto coldProductImgDto: imgList) {
                coldProductImgDto.setProductId(productId);
                coldProductMapper.saveProductImg(coldProductImgDto);
            }
        }
    }

    @CacheEvict(value = Constant.PORDUCT_CACHE_LIST,allEntries=true)
    @Transactional
    @Override
    public void saveParameter(Long productId,List<ColdProductParameterDto> parameterList) {
        if(CollectionUtils.isNotEmpty(parameterList)){
            for (ColdProductParameterDto coldProductParameterDto: parameterList) {
                coldProductParameterDto.setProductId(productId);
                coldProductMapper.saveProductParameter(coldProductParameterDto);
                //添加规格详情
                List<ColdProductParameterDetailDto> parameterDetailList = coldProductParameterDto.getColdProductParameterDetail();
                if(CollectionUtils.isNotEmpty(parameterDetailList)){
                    for (ColdProductParameterDetailDto coldProductParameterDetailDto : parameterDetailList) {
                        coldProductParameterDetailDto.setParameterId(coldProductParameterDto.getId());
                        coldProductMapper.saveProductParameterDetail(coldProductParameterDetailDto);
                    }
                }
            }
        }
    }

    @CacheEvict(value = Constant.PORDUCT_CACHE_LIST,allEntries=true)
    @Override
    public void saveParameterRule(List<ColdProductParameterRuleDto> coldProductParameterRule) {
        if(CollectionUtils.isNotEmpty(coldProductParameterRule)){
            for (ColdProductParameterRuleDto ruleDto: coldProductParameterRule) {
                coldProductMapper.setProductParameterRule(ruleDto);
            }
        }
    }

    @Override
    public List<ColdProductDto> getProductList(ColdProductQueryDto coldProductQueryDto) {
        List<ColdProductDto> productList = new ArrayList<>();
        List<ColdProductDto> list = coldProductMapper.getProductList(coldProductQueryDto);
        for (ColdProductDto coldProductDto:list) {
            productList.add(this.getProduct(coldProductDto.getProductId()));
        }
        return productList;
    }

    @Override
    public ColdProductDto getProduct(Long productId) {
        //查询基本信息
        ColdProductDto coldProductDto = coldProductMapper.getProductBase(productId);
        //查询标签信息
        List<ColdProductTagDto> tagList = coldProductMapper.getProductTag(productId);
        coldProductDto.setColdProductTag(tagList);
        //查询轮播图信息
        List<ColdProductImgDto> imgList = coldProductMapper.getProductImg(productId);
        coldProductDto.setColdProductImg(imgList);
        //查询规格信息
        List<ColdProductParameterDto> ParameterList = coldProductMapper.getProductParameter(productId);
        if(CollectionUtils.isNotEmpty(ParameterList)){
            for (ColdProductParameterDto coldProductParameterDto:ParameterList) {
                List<ColdProductParameterDetailDto> parameterDetailList = coldProductMapper.getProductParameterDetail(coldProductParameterDto.getId());
                coldProductParameterDto.setColdProductParameterDetail(parameterDetailList);
            }
        }
        coldProductDto.setColdProductParameter(ParameterList);
        return coldProductDto;
    }

    @CacheEvict(value = Constant.PORDUCT_CACHE_LIST,allEntries=true)
    @Transactional
    @Override
    public void updateProduct(ColdProductDto coldProductDto) {
        //更新基本信息
        coldProductMapper.updateProductBase(coldProductDto);
        //更新标签信息
        coldProductMapper.deleteProductTag(coldProductDto.getProductId());
        this.saveProductTag(coldProductDto.getProductId(),coldProductDto.getColdProductTag());
        //更新轮播图信息
        coldProductMapper.deleteProductImg(coldProductDto.getProductId());
        this.saveProductImg(coldProductDto.getProductId(),coldProductDto.getColdProductImg());
    }

    @CacheEvict(value = Constant.PORDUCT_CACHE_LIST,allEntries=true)
    @Override
    public void updateParameter(Long productId, List<ColdProductParameterDto> parameterList, Long zzjgId) {
        //更新规格信息
        coldProductMapper.deleteProductParameterDetail(productId);
        coldProductMapper.deleteProductParameter(productId);
        this.saveParameter(productId,parameterList);
        cacheComponent.del(CacheConst.CATEGORY_THREE_LEVEL_TREE+"_admin:"+zzjgId);
        cacheComponent.del(CacheConst.CATEGORY_THREE_LEVEL_TREE+"_app:"+zzjgId);

    }

    @CacheEvict(value = Constant.PORDUCT_CACHE_LIST,allEntries=true)
    @Override
    public boolean removeById(Serializable id) {
        return super.removeById(id);
    }
}
